package com.qinxueit.admin.api;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.crypto.SecureUtil;
import com.qinxueit.admin.entity.Article;
import com.qinxueit.admin.entity.Chapter;
import com.qinxueit.admin.entity.Sys;
import com.qinxueit.admin.utils.CustomIdGenerator;
import com.qinxueit.admin.utils.UserAgentUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class ParseHtml {

    public static Document getHtmlContent(String parseUrl) throws IOException {

        String userAgent = UserAgentUtil.randomUserAgent();

        //获取页面document对象
        Document document = Jsoup.connect(parseUrl)
                .userAgent(userAgent)
                .ignoreHttpErrors(true)
                .followRedirects(true)
                .timeout(100000)
                .ignoreContentType(true)
                .get();

        if (document.head().select("head").text().equals("出现错误！")) {
            return null;
        }
        return document;
    }

    public static Article getArticle(Document document) {

        // 简介
        String briefIntro = document.head().select("meta").eq(7).attr("content");
        // 封面
        String frontCover = document.head().select("meta").eq(8).attr("content");
        // 分类
        String bookType = document.head().select("meta").eq(9).attr("content");
        // 作者
        String author = document.head().select("meta").eq(10).attr("content");
        // 书名
        String bookName = document.head().select("meta").eq(11).attr("content");
        // url
        String bookUrl = document.head().select("meta").eq(12).attr("content");
        // 更新日期
        String updateTime = document.head().select("meta").eq(15).attr("content");
        LocalDateTime localDateTime = LocalDateTimeUtil.of(DateUtil.parse(updateTime));

        Article article = new Article();
        Long articleId = new CustomIdGenerator().nextId(article);

        article.setArticleId(articleId);
        article.setBookName(bookName);
        article.setAuthor(author);
        article.setCreateTime(localDateTime);
        article.setUpdateTime(localDateTime);
        article.setBookUrl(bookUrl);
        //article.setSource();
        article.setFrontCover(frontCover);
        article.setBriefIntro(briefIntro);
        article.setBookType(bookType);
        //System.out.println(article);


        return article;
    }

    public static List<Chapter> getChapter(Document document, Article article) {
        Elements elements = document.getElementById("list").getElementsByTag("dd");

        List<Chapter> chapterList = new ArrayList<>();

        Sys sysConfig = SysConfig.getSysVal();
        String sysUrl = sysConfig.getSysValue();

        for (Element element : elements) {
            // 章节名
            String chapterName = element.getElementsByTag("a").text();
            // 待爬取的页面
            String sourceUrl = element.getElementsByTag("a").attr("href");

            Chapter chapter = new Chapter();
            chapter.setArticleId(article.getArticleId());
            chapter.setSourceUrl(sysUrl + sourceUrl);
            chapter.setChapterName(chapterName);
            chapterList.add(chapter);
        }

        return chapterList;
    }

    public static Chapter getChapterList(Chapter chapter) throws IOException {

        String userAgent = UserAgentUtil.randomUserAgent();
        Document document = Jsoup.connect(chapter.getSourceUrl())
                .userAgent(userAgent)
                .ignoreHttpErrors(true)
                .followRedirects(true)
                .timeout(100000)
                .ignoreContentType(true)
                .get();

        if(document.head().select("meta").size() > 0){
            String content = document.getElementById("content").text();
            //System.out.println("小说内容是："+content);
            chapter.setId(chapter.getId());
            chapter.setUrlStatus(1);
            chapter.setChapterContent(content);
            chapter.setCreateTime(LocalDateTime.now());
            chapter.setUpdateTime(LocalDateTime.now());
            return chapter;
        }
        return null;
    }
}
